package javase.javase02.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * @author: Gan
 * @date 2021/4/24 0024 10:24
 * @description:
 **/
public class ReflectTestPerson {
    public static void main(String[] args) throws ClassNotFoundException,
            NoSuchMethodException, IllegalAccessException,
            InvocationTargetException, InstantiationException {
        // 获取字节码文件对象
        Class personClass = Class.forName("javase.javase02.reflect.Person");
        // 获取所有public构造方法
        // Constructor[] getConstructors()
        System.out.println("-----------获取所有public构造方法-------------");
        Constructor[] constructors = personClass.getConstructors();
        for (Constructor c : constructors) {
            System.out.println(c);
        }

        System.out.println("-----------------获取所有构造方法--------------");
        Constructor[] declaredConstructors = personClass.getDeclaredConstructors();
        for (Constructor c : constructors) {
            System.out.println(c);
        }

        System.out.println("----------------获取指定的构造方法--------------");
        Constructor constructor = personClass.getConstructor(String.class, int.class, boolean.class);
        System.out.println(constructor);

        //  System.out.println("--------------获取指定的非public构造方法---------");
        /* Constructor declaredConstructor = personClass.getDeclaredConstructor(int.class);
        declaredConstructor.setAccessible(true);
        // setAccessible(true)
        System.out.println(declaredConstructor);*/

        System.out.println("-------------通过public构造方法进行实例化---------");
        Person p1 = (Person) constructor.newInstance("李四", 23, true);
        System.out.println(p1);
    }
}